<!-----------修改密码页面------------->
<template>
  <div>
    <topheader :toptitle="toptitle"/>
    <div class="wrapper findbackpwd-byPhone">
      <div class="wrapper-inner">
        <img class="xy-logo" src="../../static/img/xy-logo.png"/>
        <div class="go-login" id="goLogin">
          <p class="pwdtip">您正在进行密码修改</p>
          <div class="xy-pwd-old">
            <input id="binduseroldPwd" name="binduseroldpwd" type="password" v-model:value="oldpwd"
                   placeholder="请输入原密码，6-12字符"/>
          </div>
          <div class="xy-pwd-new">
            <input id="bindusernewPwd" name="bindusernewpwd" type="password" v-model:value="newpwd"
                   placeholder="请输入新密码，6-12字符"/>
          </div>
          <div class="xy-pwd-sure">
            <input id="bindusernewPwd2" name="bindusernewpwd2" type="password" v-model:value="surenewpwd"
                   placeholder="请确认密码"/>
          </div>
          <div class="btn btn-normalsize btn-bluebg btn-sure" @click="surechange">确定</div>
          <div class="btn btn-normalsize btn-bluebd byphonebind" id="byphoneBind" @click="changepwdBybindmobile">
            通过绑定手机号修改
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Vue from "vue"
  import topheader from "@/components/topheader"
  import Message from "iview/src/components/message"
  import apiConfig from "../config/api.js"
  import md5 from "js-md5"

  Vue.prototype.$Message = Message
  export default {
    name: "changepwd",
    components: {
      "topheader": topheader
    },
    data() {
      return {
        toptitle: "修改密码",
        getUserInfo: {},
        oldpwd: "",
        newpwd: "",
        surenewpwd: ""
      }
    },
    created() {


    },
    methods: {
      surechange: function () {
        if (this.oldpwd === "") {
          this.$Message.warning("请输入原密码")
          return
        } else if (this.oldpwd !== "" && !/^[A-Za-z0-9]{6,12}$/.test(this.oldpwd)) {
          this.$Message.warning("密码格式错误，请输入6-12字符密码")
          return
        } else if (this.newpwd === "") {
          this.$Message.warning("请输入新密码")
          return
        } else if (this.newpwd !== "" && !/^[A-Za-z0-9]{6,12}$/.test(this.newpwd)) {
          this.$Message.warning("密码格式错误，请输入6-12字符密码")
          return
        } else if (this.surenewpwd === "") {
          this.$Message.warning("请确认密码")
          return
        } else if (this.surenewpwd !== "" && !/^[A-Za-z0-9]{6,12}$/.test(this.surenewpwd)) {
          this.$Message.warning("请确认新密码")
          return
        } else if (this.newpwd !== this.surenewpwd) {
          this.$Message.warning("两次输入的密码不一致")
          return
        } else {
					// 请求参数
          let params = {
            appid: userInfoKit.appid,
            token: userInfoKit.getUserInfo().token,
            oldPassword: md5(this.oldpwd),
            newPassword: md5(this.newpwd),
            repeatPassword: md5(this.surenewpwd)
          }					
					// 请求修改密码接口
	        this.$http({
	              method: "GET",
	              url: apiConfig.app.user.modifyPwd,
	              params: params,
	              emulateJSON: true
            }).then(function (resp) {
	            if (resp.body.code == "200") {
	            	//修改密码成功后调用获取用户信息接口
								let params = {
				            appid: userInfoKit.appid,
				            token: userInfoKit.getUserInfo().token
			        	}
				        // 请求获取用户信息接口
				        this.$http({
				              method: "GET",
				              url: apiConfig.app.user.userInfor,
				              params: params,
				              emulateJSON: true
			            }).then(function (resp) {
				            if (resp.body.code == "200") {
				              //保存用户信息
				              userInfoKit.save(resp.body)
				              //提交成功后跳转到密码修改成功页面
				              this.$router.push('/changepwdsuccess');
				            } else {
				              this.$Message.warning(resp.body.msg)
				            }
				        })
	            } else {
	              this.$Message.warning(resp.body.msg)
	            }
	        })
        }
      },
      changepwdBybindmobile: function () {
				//请求参数
				let params = {
          appid: userInfoKit.appid,
          account: userInfoKit.getUserInfo().account
        }
        //请求修改密码（发送验证码接口）
        this.$http({
          method: "GET",
          url: apiConfig.app.user.findPwdByAccount,//需修改
          params: params,
          emulateJSON: true
        }).then(function (resp) {
          if (resp.body.code == "200") {
            // 跳转到修改密码页面
            let mobile = resp.body.data.mobile
            this.$router.push({name: "changepwdbymobile", params: {mobile: mobile, account: userInfoKit.getUserInfo().account}});//需修改
          } else if (resp.body.code == "404") {
            let $this = this
            $this.$Message.warning(resp.body.msg)
            setTimeout(function () {
              // 跳转到未绑定手机号页面
              $this.$router.push("/changepwdnobindphone")
            }, 800)
          }else{
          		this.$Message.warning(resp.body.msg)
          }
        })







      }
    }
  }
</script>
